﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace StarESHelper
{
    public class EncodeHelper
    {
        /// <summary>
        /// 默认解压密码
        /// </summary>
        public static string encryptkey = "Star8521!@#$";
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="text"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Encrypt(string text, string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                key = encryptkey;
            }
            StringBuilder stringBuilder = new StringBuilder();
            var dESCryptoServiceProvider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(text);
            byte[] bytes2 = Encoding.ASCII.GetBytes(key);
            dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(key);
            dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(key);
            using (var memoryStream = new MemoryStream())
            {
                using (var cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cryptoStream.Write(bytes, 0, bytes.Length);
                    cryptoStream.FlushFinalBlock();
                    byte[] array = memoryStream.ToArray();
                    foreach (byte b in array)
                    {
                        stringBuilder.AppendFormat("{0:X2}", b);
                    }
                }
            }
            return stringBuilder.ToString();
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="cyphertext"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Decrypt(string cyphertext, string key)
        {
            if (string.IsNullOrEmpty(cyphertext))
            {
                return string.Empty;
            }
            if (string.IsNullOrWhiteSpace(key))
            {
                key = encryptkey;
            }
            var dESCryptoServiceProvider = new DESCryptoServiceProvider();
            byte[] array = new byte[cyphertext.Length / 2];
            for (int i = 0; i < cyphertext.Length / 2; i++)
            {
                int num = Convert.ToInt32(cyphertext.Substring(i * 2, 2), 16);
                array[i] = (byte)num;
            }
            dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(key);
            dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(key);
            using (var memoryStream = new MemoryStream())
            {
                using (var cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cryptoStream.Write(array, 0, array.Length);
                    cryptoStream.FlushFinalBlock();
                    return Encoding.GetEncoding("UTF-8").GetString(memoryStream.ToArray());
                }
            }
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string MD5_32(string str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            MD5 mD = MD5.Create();
            byte[] array = mD.ComputeHash(bytes);
            StringBuilder stringBuilder = new StringBuilder();
            byte[] array2 = array;
            foreach (byte b in array2)
            {
                stringBuilder.Append(b.ToString("x2"));
            }
            return stringBuilder.ToString();
        }
    }
}
